//
//  KYVideoCheckBoxLabel.m
//  KYVideoModule
//
//  Created by leoking870 on 2017/9/30.
//

#import "KYVideoCheckBoxLabel.h"
#import "KYVideoCheckBox.h"
#import "Masonry.h"
#import "NSArray+MasonryAdditiion.h"
@interface KYVideoCheckBoxLabel ()
@property (nonatomic, strong) KYVideoCheckBox *checkBox;
@property (nonatomic, strong) UILabel *textLabel;

@end


@implementation KYVideoCheckBoxLabel

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.checkBox];
        [self addSubview:self.textLabel];
        [self.subviews mas_alignAlongAxis:MASAxisTypeHorizontal fixedSpace:@8 leadSpace:8 tailSpace:8];
        [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.greaterThanOrEqualTo(self.mas_top);
            make.centerY.equalTo(self.mas_centerY);
        }];
        
        [self.checkBox mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.mas_top).with.priorityLow();
            make.centerY.equalTo(self.mas_centerY);
            make.width.mas_equalTo(CGRectGetHeight(frame));
            make.height.mas_equalTo(CGRectGetHeight(frame));
        }];
        
        [self.checkBox addTarget:self action:@selector(onValueChanged:) forControlEvents:UIControlEventValueChanged];
    }
    return self;
}

- (void)onValueChanged:(id)sender {
    if (self.checkBoxStateChanged) {
        self.checkBoxStateChanged(self.checkBox.on);
    }
}

- (void)setText:(NSString *)text {
    self.textLabel.text = text;
}
- (NSString *)text {
    return self.textLabel.text;
}

- (UIColor *)textColor {
    return self.textLabel.textColor;
}
- (void)setTextColor:(UIColor *)textColor {
    self.textLabel.textColor = textColor;
}

- (UIFont *)textFont {
    return self.textLabel.font;
}
- (void)setTextFont:(UIFont *)textFont
{
    self.textLabel.font = textFont;
}

- (void)setCheckBoxSize:(CGSize)checkBoxSize {
    self.checkBox.tf_size = checkBoxSize;
    [self.checkBox mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(checkBoxSize.width);
        make.height.mas_equalTo(checkBoxSize.height);
    }];
    [self layoutIfNeeded];
    [self setNeedsLayout];
    [self.checkBox setNeedsDisplay];
}

- (CGSize)checkBoxSize {
    return self.checkBox.tf_size;
}
- (void)setOn:(BOOL)on {
    self.checkBox.on = on;
}

- (BOOL)on {
    return self.checkBox.on;
}

- (void)setCheckBoxLineWidth:(CGFloat)checkBoxLineWidth{
    self.checkBox.lineWidth = checkBoxLineWidth;
}
- (CGFloat)checkBoxLineWidth {
    return self.checkBox.lineWidth;
}

- (CGFloat)checkBoxCornerRadius {
    return self.checkBox.cornerRadius;
}

- (void)setCheckBoxCornerRadius:(CGFloat)checkBoxCornerRadius {
    self.checkBox.cornerRadius = checkBoxCornerRadius;
}

- (void)setCheckBoxTintColor:(UIColor *)checkBoxTintColor {
    self.checkBox.tintColor = checkBoxTintColor;
}

- (UIColor *)checkBoxTintColor {
    return self.checkBox.tintColor;
}

- (void)setCheckBoxOnTintColor:(UIColor *)checkBoxOnTintColor {
    self.checkBox.onTintColor = checkBoxOnTintColor;
}

- (UIColor *)checkBoxOnTintColor {
    return self.checkBox.onTintColor;
}

- (CGSize)checkBoxMinimumTouchSize {
    return self.checkBox.minimumTouchSize;
}

- (void)setCheckBoxMinimumTouchSize:(CGSize)checkBoxMinimumTouchSize {
    self.checkBox.minimumTouchSize = checkBoxMinimumTouchSize;
}

- (UILabel *)textLabel {
    if (!_textLabel) {
        _textLabel = [[UILabel alloc] init];
        _textLabel.textColor = [UIColor blackColor];
        _textLabel.font = [UIFont systemFontOfSize:14];
    }
    return _textLabel;
}

- (KYVideoCheckBox *)checkBox {
    if (!_checkBox) {
        _checkBox = [[KYVideoCheckBox alloc]initWithFrame:CGRectMake(0, 0, 22, 22)];
        _checkBox.on = NO;
    }
    return _checkBox;
}

@end
